import Vue from 'vue'
import types from './types'

export default {
  state: {
  },
  mutations: {
    [types.SAVE_USERINFO] (state, data) {
      window.localStorage.setItem('data', JSON.stringify(data))
      Object.assign(state, data)
    },
    [types.DEL_USERINFO] (state) {
      window.localStorage.removeItem('data')
      Object.keys(state).forEach(k => Vue.delete(state, k))
    },
    [types.SET_TOKEN] (state) {
      const user = JSON.parse(window.localStorage.getItem('data'))
      Object.assign(state, user)
    }
  },
  actions: {
    [types.SAVE_USERINFO] ({commit}, data) {
      commit(types.SAVE_USERINFO, data)
    },
    [types.DEL_USERINFO] ({commit}) {
      commit(types.DEL_USERINFO)
    },
    [types.SET_TOKEN] ({commit}) {
      commit(types.SET_TOKEN)
    }
  }
}
